package com.yy.ant.sys.service.impl;

import com.yy.ant.chat.entity.Flock;
import com.yy.ant.chat.entity.Flockrefuser;
import com.yy.ant.chat.entity.Friends;
import com.yy.ant.chat.entity.Group;
import com.yy.ant.chat.mapper.FlockMapper;
import com.yy.ant.chat.mapper.FlockrefuserMapper;
import com.yy.ant.chat.mapper.FriendsMapper;
import com.yy.ant.chat.mapper.GroupMapper;
import com.yy.ant.sys.entity.User;
import com.yy.ant.sys.mapper.UserMapper;
import com.yy.ant.sys.service.IUserService;
import com.yy.model.ResultMsg;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tio.utils.json.Json;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyingying
 * @since 2019-12-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private GroupMapper groupMapper;

	@Autowired
	private FriendsMapper friendsMapper;

	@Autowired
	private FlockrefuserMapper flockrefuserMapper;

	@Autowired
	private FlockMapper flockMapper;

	@Override
	public void online(Long id) {
		User user = new User();
		user.setId(id);
		user.setStatus("online");
		userMapper.updateById(user);
	}

	@Override
	public void hide(Long id) {
		// TODO Auto-generated method stub
		User user = new User();
		user.setId(id);
		user.setStatus("offline");
		userMapper.updateById(user);
	}

	@Override
	public ResultMsg getInitData(String id, String url) {
		ResultMsg resultMsg = new ResultMsg();
		Map<String, Object> wrap = new HashMap<String, Object>();// 包装
		// 我的信息
		User user = userMapper.selectById(id);
		user.setAvatar(url + user.getAvatar() + ".jpg");
		wrap.put("mine", user);
		// 我的好友
		List<Group> groups = groupMapper.selectList(new QueryWrapper<Group>().eq("user_id", user.getId()));
		for (Group o : groups) {
			List<Friends> friends = friendsMapper.selectList(new QueryWrapper<Friends>().eq("groupid", o.getId()));
			List<User> temp = new ArrayList<User>();
			for (Friends e : friends) {
				// 查询用户信息
				User user1 = userMapper.selectById(e.getFriend());
				if (user1 != null) {
					if (StringUtils.isNotBlank(user1.getAvatar())) {
						user1.setAvatar(url + user1.getAvatar() + ".jpg");
					}
					temp.add(user1);
				}
			}
			o.setList(temp);
		}
		wrap.put("friend", groups);
		// 我的群
		List<Flockrefuser> refUsers = flockrefuserMapper.selectList(new QueryWrapper<Flockrefuser>().eq("uid", id));
		List<Flock> fs = new ArrayList<Flock>();
		for (Flockrefuser u : refUsers) {
			Flock fetch = flockMapper.selectById(u.getFid());
			fetch.setAvatar(url + fetch.getAvatar() + ".jpg");
			fs.add(fetch);
		}
		wrap.put("group", fs);
		resultMsg.setData(wrap);
		return resultMsg;
	}

	@Override
	public void setGroupFlock(User user) {
		// 创建分组
		Group group = new Group();
		group.setGroupname("我的好友");
		group.setUserId(user.getId());
		groupMapper.insert(group);
		//查询初始群是否存在，存在则加入
		Flock flock=flockMapper.selectById(1);
		if (flock!=null) {
			Flockrefuser flockrefuser=new Flockrefuser();
			flockrefuser.setFid(1);
			flockrefuser.setUid(user.getId());
			flockrefuserMapper.insert(flockrefuser);
		}
	}
}
